<div class="layuimini-container layuimini-page-anim" id="openDiv">
    <div class="layuimini-main" id="tabelNew">
        <fieldset class="table-search-fieldset">
            <legend style="color: #1c83ea">搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="formOpen">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userNameSearch" autocomplete="off" class="layui-input"
                                       id="userName">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="authority-search-btn"
                                    style="margin-left:10px;width:90px;height:38px;border:none;outline:none;border-radius:6px;background-color:#007bff;color:white"><i
                                    class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <fieldset class="table-search-fieldset">
            <legend style="color: #1c83ea">所有人员</legend>
            <form class="layui-form layui-form-pane" action="">
                <div class="layui-form-item" id="allPerson"></div>
            </form>
        </fieldset>
        <fieldset class="table-search-fieldset">
            <legend style="color: #1c83ea">已选成员</legend>

            <form class="layui-form layui-form-pane" action="">
                <div class="layui-form-item" id="members">
                    <div class="layui-btn-container"></div>
                </div>
            </form>
        </fieldset>
    </div>
</div>

<style>
    .layui-tab-title .layui-this{
        background-color: #3f7dff;
        color:#f1f4f5;
    }
</style>

<script>
    var initMemberTableParams = {
        "account": "",
        "user_name":"",
        "orderType": "account",
        "orderValue": "asc",
        "page": 1,
        "pageSize": 10,
        "role":$('#roleNewValue').val(),
        "type":""
    }

    function searchValues(res) {
        debugger
        var role = $('#roleNewValue').val();
        if(res.length > 0){
            allData = res[1];/* 所有人员*/
            $("#allPerson").children().remove();
            for(var i = 0; i < allData.count; i++){
                if(role == allData.list[i].role_id){
                    $("#allPerson").append('<input type="checkbox" lay-skin="primary" id="memberID_'+allData.list[i].id+'" lay-filter="checkMem" value="'+allData.list[i].id+'" title="'+ allData.list[i].user_name +'" name="'+ allData.list[i].user_name +'" checked>')
                }
                else{
                    $("#allPerson").append('<input type="checkbox" lay-skin="primary" id="memberID_'+allData.list[i].id+'" lay-filter="checkMem" value="'+allData.list[i].id+'" title="'+ allData.list[i].user_name +'" name="'+ allData.list[i].user_name +'">')
                }
            }

            members = res[0];/* 已选成员*/
            $("#members").children().remove();
            for(var m = 0; m < members.count; m++){
                $("#members").append('<button type="button" class="layui-btn layui-btn-sm layui-btn-primary" id="'+members.list[m].id+'" style="border: transparent;font-size: 14px" onclick="moveMember('+members.list[m].id+')"><i class="layui-icon"></i> '+members.list[m].user_name+'</button>')
            }

            layui.use('form',function () {
                var form =  layui.form;
                form.render('checkbox',);
            })
        }
    }

    $(function(){
        shared.ajaxGet('/sys-mgr/userListNew',
            initMemberTableParams,
            function callback(res) {
                searchValues(res);
            })
    })

    /*搜索*/
    layui.form.on('submit(authority-search-btn)',function (data) {
        debugger
        var role = $('#roleNewValue').val();
        shared.ajaxGet('/sys-mgr/userListNew',
            {
                "account":"",
                "user_name":$('#userName').val(),
                "orderType": "account",
                "orderValue": "asc",
                "page": 1,
                "pageSize": 10,
                "role":$('#roleNewValue').val(),
                "type":""
            },
            function callback(res) {
                searchValues(res);
            })
        return false
    })

    /*勾选人员*/
    layui.form.on('checkbox(checkMem)',function(data){
        debugger
        var role= $('#roleNewValue').val();
        var type = "";
        var dataValue = data.value;
        if(data.elem.checked){
            type = "add";
        }
        else{
            type = "del"
        }

        shared.ajaxPost('/sys-mgr/addMember', { "user_id": dataValue,"role_id":role, "type":type}, function callback(res) {
            if (res) {
                layer.msg(res, {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                });
            }
            else{
                shared.ajaxGet('/sys-mgr/userListNew',
                    {
                        "account":"",
                        "user_name":$('#userName').val(),
                        "orderType": "account",
                        "orderValue": "asc",
                        "page": 1,
                        "pageSize": 10,
                        "role":$('#roleNewValue').val(),
                        "type":""
                    },
                    function callback(res) {
                        searchValues(res);
                    })
            }
        })
    })

    /*弹出框移除已选成员*/
    function moveMember(user_id){
        debugger
        var role= $('#roleNewValue').val();
        shared.ajaxPost('/sys-mgr/moveMember', { "user_id": user_id,"role_id":role}, function callback(res) {
            debugger
            if (res) {
                layer.msg(res, {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                });
            }
            else{
                shared.ajaxGet('/sys-mgr/userListNew',
                    {
                        "account":"",
                        "user_name":$('#userName').val(),
                        "orderType": "account",
                        "orderValue": "asc",
                        "page": 1,
                        "pageSize": 10,
                        "role":$('#roleNewValue').val(),
                        "type":""
                    },
                    function callback(res) {
                        searchValues(res);
                    })
            }
        })
    }
</script>
